﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using CKEditor.NET;
using Hematology.Data;
using Hematology.Tools;
using Hematology.UserSite.Controls;

namespace Hematology.UserSite.admin
{
	public partial class FaqManagement : NgoPage
	{
		#region Fields

		private const string ListUrl = "/admin/FaqManagement.aspx?view=List";

		#endregion

		private EnumHelper.PageSection CurrentPageSection
		{
			get
			{
				return ViewState["CurrentPageSection"] != null
						   ? (EnumHelper.PageSection)Enum.Parse(typeof(EnumHelper.PageSection), ViewState["CurrentPageSection"].ToString())
						   : EnumHelper.PageSection.List;
			}
			set { ViewState["CurrentPageSection"] = value; }
		}

		private FaqItem CurrentFaqItem
		{
			get { return (FaqItem)ViewState["CurrentFAQItem"]; }
			set { ViewState["CurrentFAQItem"] = value; }
		}

		private void FillViewStateVariables()
		{
			string pageSection = Request.QueryString["view"];
			if (Array.IndexOf(Enum.GetNames(typeof(EnumHelper.PageSection)), pageSection) > -1)
			{
				CurrentPageSection =
					(EnumHelper.PageSection)Enum.Parse(typeof(EnumHelper.PageSection), pageSection);
			}
		}

		private void ToolbarSetup(CKEditorControl targetEditor)
		{
			targetEditor.config.skin = "office2003";
			targetEditor.config.toolbar = PageHelper.EditorButtonSet;

			targetEditor.DisableObjectResizing = true;
			targetEditor.ResizeEnabled = false;
		}

		private void FillEditForm()
		{
			ToolbarSetup(ckedQuestion);
			ToolbarSetup(ckedAnswer);
			
			string sFaqId = Request.QueryString["id"];
			int iFaqId;
			if (!String.IsNullOrEmpty(sFaqId) && Int32.TryParse(sFaqId, out iFaqId))
			{
				FaqItem faqItem = FaqItem.Get(iFaqId);
				if (faqItem != null)
				{
					CurrentFaqItem = faqItem;

					ckedQuestion.Text = faqItem.Question;
					ckedAnswer.Text = faqItem.Answer;
				}
			}
		}

		private void FillFaqList()
		{
			List<FaqItem> lFaqItem = FaqItem.GetFaqItems();

			rptFaqItem.DataSource = lFaqItem;
			rptFaqItem.DataBind();
		}

		protected void btnSave_Click(object sender, EventArgs e)
		{
			if (CurrentPageSection == EnumHelper.PageSection.Add)
			{
				FaqItem newFaqItem = new FaqItem
				{
					Question = ckedQuestion.Text.Trim(),
					Answer = ckedAnswer.Text.Trim(),
				};

				newFaqItem.Save();

				if (newFaqItem.FaqItemId != null)
					Response.Redirect(ListUrl);
			}

			if (CurrentPageSection == EnumHelper.PageSection.Edit)
			{
				CurrentFaqItem.Question = ckedQuestion.Text.Trim();
				CurrentFaqItem.Answer = ckedAnswer.Text.Trim();

				CurrentFaqItem.UpdateInstance();

				Response.Redirect(ListUrl);
			}
		}

		protected void btnList_Click(object sender, EventArgs e)
		{
			Response.Redirect(ListUrl);
		}

		private void FillPage()
		{
			switch (CurrentPageSection)
			{
				case EnumHelper.PageSection.Edit:
				case EnumHelper.PageSection.Add:
					mvFaq.ActiveViewIndex = 1;
					FillEditForm();

					break;
				default:
					mvFaq.ActiveViewIndex = 0;
					FillFaqList();

					break;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (Page.IsPostBack)
				return;
			
			FillViewStateVariables();
			FillPage();
		}

		protected void rptFaqItem_ItemCommand(object source, RepeaterCommandEventArgs e)
		{
			if (e.CommandName == "DeleteFaqItem")
			{
				int iFaqItemId = Convert.ToInt32(e.CommandArgument);
				FaqItem faqItem = FaqItem.Get(iFaqItemId);
				faqItem.IsDeleted = true;

				faqItem.UpdateInstance();

				FillFaqList();
			}
		}
	}
}